import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/network/UploadFilesUtils.dart';
import 'package:flutter_app/ui/WebViewPage.dart';
import 'package:flutter_app/ui/collect/photo_select/photo_select_view.dart';
import 'package:flutter_app/ui/collect/query_list2/query_detail/query_detail_view.dart';
import 'package:flutter_app/ui/main/main_logic.dart';
import 'package:flutter_app/utils/ImagePickerUtils.dart';
import 'package:flutter_app/utils/ImagePickersUtils.dart';
import 'package:flutter_app/utils/amap_utils.dart';
import 'package:flutter_app/utils/date_time_utils.dart';
import 'package:flutter_app/utils/device_info_utils.dart';
import 'package:flutter_app/utils/file_picker_utils.dart';
import 'package:flutter_app/utils/logger_utils.dart';
import 'package:flutter_app/utils/md5_file_utils.dart';
import 'package:flutter_app/utils/path_provider_utils.dart';
import 'package:flutter_app/utils/permission_utils.dart';
import 'package:flutter_app/utils/queue_utils.dart';
import 'package:flutter_app/utils/timer_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/CustomDialog.dart';
import 'package:flutter_app/widget/ImagesGridviewWidget.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_app/widget/image_watermark.dart';
import 'package:get/get.dart';

import 'queue_logic.dart';

/**
 * @Description: 消息队列
 * @Author: 下雨了
 * @CreateDate: 2022-6-2 14:40:34
 * @Version: 1.0
 */
///
class QueuePage extends StatefulWidget {
  const QueuePage({Key? key}) : super(key: key);

  @override
  _QueuePageState createState() => _QueuePageState();
}

class _QueuePageState extends State<QueuePage> {
  final logic = Get.put(QueueLogic());
  final state = Get.find<QueueLogic>().state;
  final mainLogic = Get.find<MainLogic>();

  var actionKey = GlobalKey();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<QueueLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF1F1F1,
        appBar: MyAppBar(context, title: "同步列表"),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: [
                  Column(
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          logic.add(logic.imagesList);
                        },
                        child: Text("add消息队列"),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          QueueUtils.get("video")?.cancelTask();
                        },
                        child: Text("取消队列"),
                      ),
                    ],
                  ),
                  Column(
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          ImagePickersUtils().selectImages(selectCount: 9, showCamera: true);
                          // ImagePickerUtils().pickImage(source: ImageSource.gallery);
                        },
                        child: Text("照片"),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          // ImagePickersUtils().selectVideos(selectCount: 9, showCamera: true);
                        },
                        child: Text("视频"),
                      ),
                      ElevatedButton(
                        onPressed: () async {
                          // ImagePickersUtils().selectAll(selectCount: 9, showCamera: false);
                          // List<XFile>? list = await ImagePickerUtils().pickMultiImage();

                          List<Media>? list = await FilePickerUtils().pickFiles();
                          UploadFilesUtils().upload(list!);

                          // String path = list?.first.path ?? "";
                          // String fileName =  list?.first.name ?? "";
                          // kLog.d(path);
                          // String path = "/data/user/0/com.jianduyuan.anhuixiaoneng/cache/file_picker/VID_20211208_10500398.mp4";
                          // String fileName = "fileName.mp4";
                          // UploadFiles().start(path, fileName);

                        },
                        child: Text("所有"),
                      ),
                    ],
                  ),
                  Column(
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          DeviceInfoUtils().getAndroidDeviceInfo().then((value) {
                            print(value.toMap());
                          });
                        },
                        child: Text("获取设备信息"),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          Get.to(() => WebViewPage(
                              title: "ptf",
                              url: "http://120.46.145.144:7199/publicPortal_organs/inspector-score-rules.pdf"));
                        },
                        child: Text("测试web view ptf"),
                      ),
                      ElevatedButton(
                        onPressed: () async {
                          print(await PermissionUtils().getPhone());
                        },
                        child: Text("getPhone"),
                      ),
                    ],
                  ),
                ],
              ),
              Container(
                child: Text(
                  '888888',
                  style: TextStyle(
                    // color: Colors.white,
                      fontSize: 20,
                      foreground: Paint()
                        ..style = PaintingStyle.stroke
                        ..strokeWidth = 1
                        ..color = Colors.red
                        ..shader = LinearGradient(
                                begin: Alignment.bottomLeft,
                                end: Alignment.topRight,
                                colors: [Colors.red, Colors.black])
                            .createShader(Rect.fromLTWH(0, 0, 300, 100))
                  ),
                ),
              ),
              Text(
                '888888',
                style: TextStyle(
                    fontSize: 14,
                    foreground: Paint()
                      ..style = PaintingStyle.stroke
                      ..strokeWidth = 0.2
                      ..color = Colors.white
                      ..shader = LinearGradient(
                              begin: Alignment.bottomLeft,
                              end: Alignment.topRight,
                              colors: [Colors.black, Colors.black])
                          .createShader(Rect.fromLTWH(0, 0, 300, 100)
                      )
                ),
              ),
              TextButton(
                onPressed: () {
                  // Get.to(WebViewPage(title: "调查问卷", url: "https://xxx.szzfyjzx.icu:5236/gzwm/index.html#/survey/45cd8c81-e331-4a4b-a3f6-cf846f7cd176"));
                  Get.to(WebViewPage(title: "调查问卷", url: "https://xxx.szzfyjzx.icu:5236/gzwm/index.html#/survey/995fb83a-8281-4278-a4d5-040a2c1796e0"));
                },
                child: Text("WebView"),
              ),
              TextButton(
                onPressed: () async{
                  print(await PathProviderUtils().getTempDir());
                },
                child: Text("getTempDir"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => QueryDetailPage(title: "问题详情"));
                },
                child: Text("问题详情"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => PhotoSelectPage(title: "选择图片/视频证据"), arguments: "123")?.then((list) {
                    print(list.toString());
                  });
                },
                child: Text("选择图片/视频证据"),
              ),
              TextButton(
                onPressed: () async {
                  LocationInfo? locationInfo =   await AMapUtils().startLocation();
                    print(locationInfo?.address);

                  // await AMapUtils((locationInfo) {
                  //   print(locationInfo.address);
                  // }).startLocation();
                  // CameraUtils().openCameraAddWatermark(dirName: "12312312");
                  // print(DateTimeUtils.formYMDHMS(DateTime.now()));
                },
                child: Text("测试"),
              ),
              TextButton(
                onPressed: () {
                  // kLog.v(PreventTapUtils().multiTouch());
                  // kLog.v(DateTimeUtils.formZone(DateTime.now()));
                  // kLog.v(DateTimeUtils.form(DateTimeUtils.FORMAT_FOUR, DateTime.now()));
                  // Get.to(BlankScreenPage());
                  // TimerUtils().startTimer();
                  showDialog(context: this.context, barrierDismissible: false, builder: (context){
                    return CustomDialog(
                      content: "",
                      onPressed: () {
                        Navigator.of(this.context).pop("refresh");
                      },
                    );
                  });
                },
                child: Text("测试"),
              ),

              BrnProgressChart(
                key: UniqueKey(),
                width: 300,
                height: 40,
                value: 0.7,
                showAnimation: true,
                brnProgressIndicatorBuilder: (BuildContext context, double value) {
                  return Text('自定义文本：$value', style: TextStyle(color: Colors.white),);
                },
              ),
              ImageWatermark(address: "河南省郑州市金水区杨金路新科技市场110号大哥哥大哥个 额额饿 ", time: "2022-8-10 09:54:38", name: "哈哈哈",),
              ListView.builder(
                shrinkWrap: true, //解决无线高度问题 解决SingleChildScrollView 和listview冲突问题 ListView.builder
                physics: NeverScrollableScrollPhysics(), //禁止滚动
                padding: EdgeInsets.only(top: 0),
                itemCount: state.taskList.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListItem(state.taskList[index]);
                },
              ),
              BrnTagCustom(
                tagText: "123123",
                fontSize: 14,
              ),
              TextButton(
                onPressed: () async {
                  await logic.start();
                },
                child: Text("open camera"),
              ),
              Container(
                padding: EdgeInsets.only(top: 15),
                child: ImagesGridviewWidget(
                  key: imagesGridViewKey,
                  isOpenPhoto: true,
                  dirName: "123",
                  isOpenFile: mainLogic.state.pinPutState.value,
                  onMediaResultsCallback: (List<Media> imagesList, filePathList) {
                    logic.imagesList = imagesList;
                  },
                ),
              ),
            ],
          ),
        ),
      );
    });
  }


  Widget ListItem(TaskInfo item) {
    return Container(
      margin: EdgeInsets.only(left: 12, right: 12, top: 12),
      padding: EdgeInsets.only(top: 12, left: 12, right: 12, bottom: 12),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4),
          color: MyColors.FFFFFFFF
      ),
      child: Column(
        children: [
          Text(" taskList = " + item.name),
          Visibility(
              visible: logic.uploadProgress.value != "",
              child: Text(logic.uploadProgress.value),)
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<QueueLogic>();
    super.dispose();
  }
}
